#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
from subprocess import Popen,PIPE
import time,string



def run(cmds):
    """ execute une liste de commandes 'cmds' et retourne sa sortie """
    p = Popen(cmds, shell=False,stdout=PIPE,stderr=PIPE)
    time.sleep(0.01)    # to avoid "IOError: [Errno 4] Interrupted system call"
    out = string.join(p.stdout.readlines() ).strip()
    return out


def normalize(valRech):
    """ remplace les caracteres stranges """
    valRech=unicode(valRech).encode("utf_16")
    sfrom = ur"àâäéèêëïîôöûùüç /\<>'|[]!?-+:;"
    sto   = ur"aaaeeeeiioouuuc_______________"
    return valRech.translate( string.maketrans(sfrom.encode("utf_16"),sto.encode("utf_16")) ).decode("utf_16")


def affDate(d):
    """ transforme une datetime 'd' en un jour lisible """
    t={
       -2:"avant-hier",
       -1:"hier",
       0:"aujourd'hui",
       1:"demain",
       2:"après-demain",
      }
    n=datetime.datetime.now()
    n=datetime.datetime(n.year,n.month,n.day,0,0,0)
    d=datetime.datetime(d.year,d.month,d.day,0,0,0)
    delta= (d-n).days
    if delta in t.keys():
        return t[delta]
    else:
        return d.strftime("%A %d %B %y")

if __name__ == "__main__":
    pass